package controller

import (
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/mvc"
	"gowork/checkWorkAttendance/model"
	"strconv"
	"time"
)

type CheckWorkAttendanceController struct {
	Ctx iris.Context
	Staff *model.Staff
}

func (cwac *CheckWorkAttendanceController)PostInsertscheduling() mvc.Result {

	cwac.Staff = new(model.Staff)

	var incomingParameters model.IncomingParameters
	cwac.Ctx.ReadJSON(&incomingParameters)

	res, err := cwac.Staff.InsertScheduling(&incomingParameters)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": res["message"], "data": nil, "code":res["code"],},
	}
}

func (cwac *CheckWorkAttendanceController)PostQueryscheduling() mvc.Result {

	cwac.Staff = new(model.Staff)

	staffIdStr := cwac.Ctx.FormValue("staffId")
	staffId, err := strconv.ParseInt(staffIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	day := cwac.Ctx.FormValue("day")

	res, err := cwac.Staff.QueryScheduling(staffId, day)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": "成功", "data": res, "code":"200",},
	}
}

func (cwac *CheckWorkAttendanceController)PostPunchclock() mvc.Result {

	cwac.Staff = new(model.Staff)

	staffIdStr := cwac.Ctx.FormValue("staffId")
	staffId, err := strconv.ParseInt(staffIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	day := cwac.Ctx.FormValue("day")

	res, err := cwac.Staff.PunchClock(staffId, day)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": res["message"], "data": nil, "code":res["code"],},
	}
}

func (cwac *CheckWorkAttendanceController)PostSingledaystatistics() mvc.Result {

	cwac.Staff = new(model.Staff)

	staffIdStr := cwac.Ctx.FormValue("staffId")
	staffId, err := strconv.ParseInt(staffIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	day := cwac.Ctx.FormValue("day")

	res, err := cwac.Staff.SingleDayStatistics(staffId, day)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": res["message"], "data": nil, "code":res["code"],},
	}
}

func (cwac *CheckWorkAttendanceController)PostMonthlysummary() mvc.Result {

	cwac.Staff = new(model.Staff)

	month := cwac.Ctx.FormValue("month")

	res, err := cwac.Staff.MonthlySummary(month)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": res["message"], "data": nil, "code":res["code"],},
	}
}

func (cwac *CheckWorkAttendanceController)PostLeave() mvc.Result {

	cwac.Staff = new(model.Staff)

	staffIdStr := cwac.Ctx.FormValue("staffId")
	staffId, err := strconv.ParseInt(staffIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	loadLocation, lErr := time.LoadLocation("Asia/Shanghai")
	if lErr != nil {
		return mvc.Response{
			Object: map[string]interface{}{"code": 400, "mesage": lErr.Error()},
		}
	}
	startTimeStr := cwac.Ctx.FormValue("startTime")
	startTime, err := time.ParseInLocation("2006-01-02 15:04:05", startTimeStr, loadLocation)
	if err != nil {
		return mvc.Response{
			Object : map[string]interface{}{"code": 400, "mesage": err.Error()},
		}
	}

	endTimeStr := cwac.Ctx.FormValue("endTime")
	endTime, err := time.ParseInLocation("2006-01-02 15:04:05", endTimeStr, loadLocation)
	if err != nil {
		return mvc.Response{
			Object : map[string]interface{}{"code": 400, "mesage": err.Error()},
		}
	}

	durationStr := cwac.Ctx.FormValue("duration")
	duration, dErr := strconv.Atoi(durationStr)
	if dErr != nil {
		return mvc.Response{
			Object : map[string]interface{}{"code": 400, "mesage": dErr.Error()},
		}
	}

	reasons := cwac.Ctx.FormValue("reasons")

	day := cwac.Ctx.FormValue("day")

	res, err := cwac.Staff.Leave(staffId, startTime, endTime, duration, reasons, day)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": res["message"], "data": nil, "code":res["code"],},
	}
}

func (cwac *CheckWorkAttendanceController)PostOvertimeapplication() mvc.Result {

	cwac.Staff = new(model.Staff)

	var overtimeApplicationRecord model.OvertimeApplicationRecord

	cwac.Ctx.ReadJSON(&overtimeApplicationRecord)

	res, err := cwac.Staff.OvertimeApplication(&overtimeApplicationRecord)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": res["message"], "data": nil, "code":res["code"],},
	}
}

func (cwac *CheckWorkAttendanceController)PostMonthlysummarythread() mvc.Result {

	cwac.Staff = new(model.Staff)

	month := cwac.Ctx.FormValue("month")

	res, err := cwac.Staff.MonthlySummaryThred(month)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": res["message"], "data": nil, "code":res["code"],},
	}
}





















